package com.example.e_comm.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.e_comm.entity.Permission;
import com.example.e_comm.entity.Role;
import com.example.e_comm.service.PermissionService;
import com.example.e_comm.service.RoleService;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/roles")
public class RoleController {
    private RoleService roleService;
    private PermissionService permissionService;

    public RoleController(RoleService _roleService
            , PermissionService _permissionService) {
        this.roleService = _roleService;
        this.permissionService = _permissionService;
    }

    @RequestMapping("/list")
    public List<Role> list() {
        return roleService.list();
    }

    //根据条件查询结果
    @GetMapping("/page")
    public IPage<Role> page(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize) {
        return roleService.page(new Page<>(pageNum, pageSize));
    }

    @GetMapping("/search")
    public IPage<Role> search(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestParam(required = false) String keyword) {
        Page<Role> page = new Page<>(pageNum, pageSize);
        return roleService.page(page, new LambdaQueryWrapper<Role>()
                .like(Role::getName, keyword)
                .or()
                .like(Role::getDescription, keyword)
                .or()
                .like(Role::getRoleKey, keyword));
    }

    @PostMapping("create")
    public Role createRole(@RequestBody Role entity){
    //检查角色是否存在
        if(roleService.getById(entity.getId())!=null)

    {
        throw new IllegalArgumentException("角色已存在!");
    }
        roleService.save(entity);
        return entity;
}
    @PostMapping("update")
    public Role updateRole(@RequestBody Role entity) {
        //检查角色是否存在
        if (roleService.getById(entity.getId()) != null) {
            throw new IllegalArgumentException("角色不存在!");
        }
        roleService.updateById(entity);
        return entity;
    }
    @PostMapping("/delete/{roleId}")
    public void deleteRole(@PathVariable Long roleId) {
        //检查角色是否存在
        if (roleService.getById(roleId) == null) {
            throw new IllegalArgumentException("角色不存在!");
        }
        roleService.removeById(roleId);
    }
    @PostMapping("/deleteBatch")
    public void deleteRole(@RequestBody List<Long> roleIds) {
        //检查角色是否存在
        List< Role> roles = roleService.listByIds(roleIds);
        if (roles.size() != roleIds.size()) {
            throw new IllegalArgumentException("角色不存在!");
        }
        roleService.removeByIds(roleIds);
    }
    //获取角色权限列表
    @GetMapping("/getPermission")
    public List<Permission> getPermissionsById(Long roleId) {
        return permissionService.getPermissionsByRoleId(roleId);
    }

    //为角色分配权限
    @PostMapping("/assignPermission")
    public void assignPermissions(
            @RequestParam Long roleId,
            @RequestParam List<Long> permissionIds
    ) {
        //角色是否存在
        Role role = roleService.getById(roleId);
        if (role == null) {
            throw new IllegalArgumentException("角色不存在!");
        }
        List<Permission> permissions = permissionService.listByIds(permissionIds);
        if (permissions.size() != permissionIds.size()){
            throw new IllegalArgumentException("请核查权限再操作!");
        }
        roleService.assignPermissions(roleId,permissionIds);
    }

}